import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)



const routes = [
  {
    path: '/',
    redirect: '/index'
  },
  {
    path: '/index',
    name: 'index',
    component: () => import('@/views/Index/Index'),
    children: [
      { path: '/infoShow', name: 'infoShow', component: () => import('@/views/infoShow') },
      { path: '/info', name: 'info', component: () => import('@/views/info') },
      { path: '/feedback', name: 'feedback', component: () => import('@/views/feedback') },
      { path: '/news', name: 'news', component: () => import('@/views/news') },
      { path: '/aquatic', name: 'aquatic', component: () => import('@/views/aquatic') },
      { path: '/fish', name: 'fish', component: () => import('@/views/fish') },
      { path: '/fish/data', name: 'fishdata', component: () => import('@/views/fish/data.vue') },
      { path: '/shell', name: 'shell', component: () => import('@/views/shell') },
      { path: '/shell/data', name: 'shelldata', component: () => import('@/views/shell/data.vue') },
      { path: '/shrimp', name: 'shrimp', component: () => import('@/views/shrimp') },
      { path: '/shrimp/data', name: 'shrimpdata', component: () => import('@/views/shrimp/data.vue') },
      { path: '/contact', name: 'contact', component: () => import('@/views/contact') },
      { path: '', component: () => import('@/views/Home/Home') },
      { path: '/home', name: 'home', component: () => import('@/views/Home/Home') },
      { path: '/introduce', name: 'introduce', component: () => import('@/views/introduce') }
    ]

  },
  // { path: '/index/home', name: 'home', component: () => import('@/views/Home/Home') }
,
  {
    path: '/register',
    name: 'register',
    component: () => import('@/views/Register/Register')
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('@/views/Login/Login')
  },
  {
    path: '*',
    name: '404',
    component: () => import('@/views/404')
  },
]




const router = new VueRouter({
  routes
})


router.beforeEach((to, from, next) => {
  // to 要到的路由  from过来的路由  next() 调用才会执行
  //做权限判断 是否可以进入主页面
  if (to.path === '/login' || to.path === '/register') {
    next()
  } else {
    const token = localStorage.getItem('token') ? true : false
    if (token) {
      next()
    } else {
      next('/login')
    }
  }
})

export default router
